package com_.dxy_test_.socket_;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;

/**
 * @Description 服务端
 * @Author dangxianyue
 * @Date 2022/9/26 12:47
 */
public class Server {
    private static final int PORT = 8080;

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(PORT);
        System.out.println("服务已经启动，端口号：" + PORT + "\n");

        Socket client = null;
        BufferedReader reader = null;
        PrintWriter writer = null;
        //循环监听
        while (true) {
            try {
                client = server.accept();

                reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
                writer = new PrintWriter(client.getOutputStream());

                //读取
                String line = reader.readLine();
                String[] lines = line.split(" ");
                String requestMethod = lines[0];
                String requestURI = lines[1];
                System.out.println("line : " + line);
                System.out.println("lines : " + Arrays.asList(lines));
                System.out.println("requestMethod : " + requestMethod);
                System.out.println("requestURI : " + requestURI + "\n\n");


                //获取输出流，拼接返回信息
                //返回请求信息
                StringBuilder responseStr = new StringBuilder();
                responseStr.append("HTTP/1.1 200 OK\n");//响应行
                if ("/".equals(requestURI)) {
                    //响应头+换行
                    responseStr.append("content-type:application/json;charset=utf-8\n\n");
                    //响应体
                    responseStr.append("{\"code\":200,\"desc\":\"请求成功，当前为根路径\",\"data\":null}");
                } else if ("/index".equals(requestURI)) {
                    //响应头+换行
                    responseStr.append("content-type:text/html;charset=utf-8\n\n");
                    //响应体
                    responseStr.append("<html>");
                    responseStr.append("<title>Abl Tomcat</title>");
                    responseStr.append("<h1> Hello </h1>");
                    responseStr.append("</html>");
                } else {
                    responseStr.append("content-type:text/html;charset=utf-8\n\n");
                    responseStr.append("404");//响应体
                }

                writer.print(responseStr);
                writer.flush();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {

                if (writer != null) {
                    try {
                        writer.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                if (reader != null) {
                    try {
                        reader.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                if (client != null) {
                    try {
                        client.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }


            }
        }


    }
}
